﻿#include "commontreedelegate.h"
#include <QComboBox>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QLineEdit>
#include <QDateTimeEdit>



CommonTreeDelegate::CommonTreeDelegate(QObject *parent) : QItemDelegate(parent)
{
    mDisableRow.clear();
    mDisableCol.clear();
}

/**
 * @brief setRowDisable 设置指定行不能够编辑
 * @param lstRow 行列表
 */
void CommonTreeDelegate::setRowDisable(QList<int> lstRow)
{
    mDisableRow.append(lstRow);
}

/**
 * @brief setColDisable 设置指定列不能够编辑
 * @param lstCol 列列表
 */
void CommonTreeDelegate::setColDisable(QList<int> lstCol)
{
    mDisableCol.append(lstCol);
}

// editing
QWidget * CommonTreeDelegate::createEditor(QWidget *parent,
                                        const QStyleOptionViewItem &option,
                                        const QModelIndex &index) const
{
    // 第一列不参与编辑
    if(mDisableCol.contains(index.column()) || mDisableRow.contains(index.row()))
        return NULL;

    QVariant vt = index.data(Qt::EditRole);
    switch(vt.type())
    {
    case QVariant::Bool:
    {
        QComboBox* editor = new QComboBox(parent);
        editor->addItem(tr("false"));
        editor->addItem(tr("true"));
        editor->setCurrentIndex(index.data().toInt());
        return editor;
    }
    case QVariant::Int:
    {
        QSpinBox* editor = new QSpinBox(parent);
        editor->setRange(-65536,65536);
        editor->setValue(index.data().toInt());
        return editor;
    }
    case QVariant::Double:
    {
        QDoubleSpinBox* editor = new QDoubleSpinBox(parent);
        editor->setDecimals(6);
        editor->setRange(-1e15,1e15);
        editor->setValue(index.data().toDouble());
        return editor;
    }
    case QVariant::Time:
    {
        QTimeEdit* editor = new QTimeEdit(parent);
        editor->setTime(index.data().toTime());
        return editor;
    }
    case QVariant::Date:
    {
        QDateEdit* editor = new QDateEdit(parent);
        editor->setDate(index.data().toDate());
        return editor;
    }
    case QVariant::DateTime:
    {
        QDateTimeEdit* editor = new QDateTimeEdit(parent);
        editor->setDate(index.data().toDateTime().date());
        editor->setTime(index.data().toDateTime().time());
        return editor;
    }
    case QVariant::String:
    {
        QLineEdit* editor = new QLineEdit(parent);
        editor->setText(index.data().toString());
        return editor;
    }
    case QVariant::StringList:
    {
        QComboBox* editor = new QComboBox(parent);
        editor->addItems(index.data().toStringList());
        if(editor->count() > 0)
            editor->setCurrentIndex(0);
        return editor;
    }
    default:

        break;
    }

    return QItemDelegate::createEditor(parent,option,index);

}

void CommonTreeDelegate::setEditorData(QWidget * widget, const QModelIndex &index) const
{
    switch(index.data().type())
    {
    case QVariant::Bool:
    {
        QComboBox* editor = qobject_cast<QComboBox*>(widget);
        if(editor)
            editor->setCurrentIndex(index.data().toInt());
        break;
    }
    case QVariant::Int:
    {
        QSpinBox* editor = qobject_cast<QSpinBox*>(widget);
        if(editor)
            editor->setValue(index.data().toInt());
        break;
    }
    case QVariant::Double:
    {
        QDoubleSpinBox* editor = qobject_cast<QDoubleSpinBox*>(widget);
        if(editor)
            editor->setValue(index.data().toDouble());
        break;
    }
    case QVariant::Time:
    {
        QTimeEdit* editor = qobject_cast<QTimeEdit*>(widget);
        if(editor)
            editor->setTime(index.data().toTime());
        break;
    }
    case QVariant::Date:
    {
        QDateEdit* editor = qobject_cast<QDateEdit*>(widget);
        if(editor)
            editor->setDate(index.data().toDate());
        break;
    }
    case QVariant::DateTime:
    {
        QDateTimeEdit* editor = qobject_cast<QDateTimeEdit*>(widget);
        if(editor)
        {
            editor->setDate(index.data().toDateTime().date());
            editor->setTime(index.data().toDateTime().time());
        }
        break;
    }
    case QVariant::String:
    {
        QLineEdit* editor = qobject_cast<QLineEdit*>(widget);
        if(editor)
        {
            editor->setText(index.data().toString());
        }
        break;
    }
    case QVariant::StringList:
    {

        QComboBox* editor = qobject_cast<QComboBox*>(widget);
        if(editor)
        {
            if(editor->count() <= 0)
                editor->addItems(index.data(Qt::EditRole).toStringList());

            if(editor->count() > 0)
                editor->setCurrentIndex(0);

        }

        break;
    }
    default:

        break;
    }
}

void CommonTreeDelegate::setModelData(QWidget *widget,
                                   QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    switch(index.data().type())
    {
    case QVariant::Bool:
    {
        QComboBox* editor = qobject_cast<QComboBox*>(widget);
        if(editor)
        {
            if(editor->currentIndex() == 0)
                model->setData(index,false);
            else
                model->setData(index,true);
        }
        break;
    }
    case QVariant::Int:
    {
        QSpinBox* editor = qobject_cast<QSpinBox*>(widget);
        if(editor)
            model->setData(index,editor->value());
        break;
    }
    case QVariant::Double:
    {
        QDoubleSpinBox* editor = qobject_cast<QDoubleSpinBox*>(widget);
        if(editor)
            model->setData(index,editor->value());
        break;
    }
    case QVariant::Time:
    {
        QTimeEdit* editor = qobject_cast<QTimeEdit*>(widget);
        if(editor)
            model->setData(index,editor->time());
        break;
    }
    case QVariant::Date:
    {
        QDateEdit* editor = qobject_cast<QDateEdit*>(widget);
        if(editor)
            model->setData(index,editor->date());
        break;
    }
    case QVariant::DateTime:
    {
        QDateTimeEdit* editor = qobject_cast<QDateTimeEdit*>(widget);
        if(editor)
        {
            model->setData(index,editor->dateTime());
        }
        break;
    }
    case QVariant::String:
    {
        QLineEdit* editor = qobject_cast<QLineEdit*>(widget);
        if(editor)
        {
            model->setData(index,editor->text());
        }
        break;
    }
    case QVariant::StringList:
    {
        QComboBox* editor = qobject_cast<QComboBox*>(widget);
        if(editor)
        {

            QStringList lstValue;
            lstValue.clear();
            for(int i=0;i<editor->count(); ++i)
            {
                lstValue.append(editor->itemText(i));
            }
            model->setData(index,lstValue,Qt::EditRole);
            model->setData(index,editor->currentText(),Qt::DisplayRole);
        }
        break;
    }
    default:

        break;
    }
}

void CommonTreeDelegate::updateEditorGeometry(QWidget *editor,
                                           const QStyleOptionViewItem &option,
                                           const QModelIndex &index) const
{
    Q_UNUSED(index)
    editor->setGeometry(option.rect);
}


